$(function(){
	// 左侧边栏
	$('.sidebar-toggle').click(function(){
		$.get($(this).attr("rel-url"));
	});
	// 右侧边栏-更换主题
	$('.list-unstyled').on('click','a',function(){
		// 可用主题
		var skins = [
			"skin-blue",
			"skin-black",
			"skin-red",
			"skin-yellow",
			"skin-purple",
			"skin-green",
			"skin-blue-light",
			"skin-black-light",
			"skin-red-light",
			"skin-yellow-light",
			"skin-purple-light",
			"skin-green-light"
		];
		
		$.each(skins,function(i) {
			$("body").removeClass(skins[i]);
		});
		
		var skin = $(this).data('skin');
		
		var aside = $(this).data('aside');
		
		var url = $(this).attr("rel-url");
		
		$("body").addClass(skin);
		
		$(".control-sidebar").removeClass('control-sidebar-dark');
		$(".control-sidebar").removeClass('control-sidebar-light');
		$(".control-sidebar").addClass(aside);
		
		$.get(url,{skin:skin});
	});
	// 设置主题
	var data = {
		skin:[
			{name:'Purple',theme:{skin:'skin-purple',aside:'control-sidebar-dark',active:'bg-purple-active',navbar:'bg-purple',side:'#222d32',content:'#f4f5f7'}}
		],
		skins:[
			{name:'Blue',theme:{skin:'skin-blue',aside:'control-sidebar-dark',active:'bg-blue-active',navbar:'bg-blue',side:'#222d32',content:'#f4f5f7'}},
			{name:'Black',theme:{skin:'skin-black',aside:'control-sidebar-dark',active:'bg-gray-active',navbar:'bg-gray',side:'#222d32',content:'#f4f5f7'}},
			{name:'Purple',theme:{skin:'skin-purple',aside:'control-sidebar-dark',active:'bg-purple-active',navbar:'bg-purple',side:'#222d32',content:'#f4f5f7'}},
			{name:'Green',theme:{skin:'skin-green',aside:'control-sidebar-dark',active:'bg-green-active',navbar:'bg-green',side:'#222d32',content:'#f4f5f7'}},
			{name:'Red',theme:{skin:'skin-red',aside:'control-sidebar-dark',active:'bg-red-active',navbar:'bg-red',side:'#222d32',content:'#f4f5f7'}},
			{name:'Yellow',theme:{skin:'skin-yellow',aside:'control-sidebar-dark',active:'bg-yellow-active',navbar:'bg-yellow',side:'#222d32',content:'#f4f5f7'}},
			{name:'Blue Light',theme:{skin:'skin-blue-light',aside:'control-sidebar-light',active:'bg-blue-active',navbar:'bg-blue',side:'#f9fafc',content:'#f4f5f7'}},
			{name:'Black Light',theme:{skin:'skin-black-light',aside:'control-sidebar-light',active:'bg-gray-active',navbar:'bg-gray',side:'#f9fafc',content:'#f4f5f7'}},
			{name:'Purple Light',theme:{skin:'skin-purple-light',aside:'control-sidebar-light',active:'bg-purple-active',navbar:'bg-purple',side:'#f9fafc',content:'#f4f5f7'}},
			{name:'Green Light',theme:{skin:'skin-green-light',aside:'control-sidebar-light',active:'bg-green-active',navbar:'bg-green',side:'#f9fafc',content:'#f4f5f7'}},
			{name:'Red Light',theme:{skin:'skin-red-light',aside:'control-sidebar-light',active:'bg-red-active',navbar:'bg-red',side:'#f9fafc',content:'#f4f5f7'}},
			{name:'Yellow Light',theme:{skin:'skin-yellow-light',aside:'control-sidebar-light',active:'bg-yellow-active',navbar:'bg-yellow',side:'#f9fafc',content:'#f4f5f7'}}
		]
	};
	$('.theme').html($('.skins').tmpl(data.skin));
	$('.themes').html($('.skins').tmpl(data.skins));
});

(function(window,$,undefined){
	// 生成随机字符
	$.generateRandom = function(option){
		var config = {
			chars:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
			length:10,// 输出长度
			delimiter:'',// 分隔符
		};
		
		option = $.extend(config,option);
		
		var random = new Array();
		
		for (i = 0; i < option.length; i++)
		{
			random.push(option.chars.charAt(Math.floor(Math.random() * option.chars.length)))
		}
		
		return random.join(option.delimiter);
	}
})(window,jQuery);

//异步提交文件类型
function uploadFile(obj)
{
	var url = $(obj).attr('rel-url')
	var id = $(obj).attr('id');
    $.ajaxFileUpload
    (
        {
            url: url, //用于文件上传的服务器端请求地址
            secureuri: false, //是否需要安全协议，一般设置为false
            fileElementId: id, //文件上传域的ID
            dataType: 'json', //返回值类型 一般设置为json
            success: function (data, status)  //服务器成功响应处理函数
            {
            	if ( data.status == true )
				{
                    $("."+data.info.filename).val(data.info.path);
					$("[name='"+data.info.name+"']").val(data.info.savename);
                }
                else
                {
					layer.msg(data.info,{
						shift: data.status ? 2 : 6
					});
                }

            },
            error: function (data, status, e)//服务器响应失败处理函数
            {
                alert(e);
            }
        }
    );
    return false;
}

//增加一对参数框
function addParamInput(obj, id, name = '')
{
	id1 = '\''+id+'\'';
	var html = '\n' +
        '\t\t\t<label class="col-sm-2 control-label"></label>\n' +
        '\t\t\t<div class="input-group">\n' +
        '\t\t\t\t<div class="col-sm-4">\n' +
        '\t\t\t\t\t<input type="text" name="param'+name+'[key][]" value="" class="form-control" placeholder="key" autocomplete="off" />\n' +
        '\t\t\t\t</div>\n' +
        '\t\t\t\t<div class="col-sm-7">\n' +
        '\t\t\t\t\t<input type="text" name="param'+name+'[value][]" value="" class="form-control" placeholder="value" autocomplete="off" />\n' +
        '\t\t\t\t</div>\n' +
        '\t\t\t\t<div class="col-sm-0">\n' +
        '\t\t\t\t\t<button type="button" onclick="addParamInput(this, '+id1+')">+</button>\n' +
        '\t\t\t\t</div>\n' +
        '\t\t\t</div>';

	$('#'+id).append(html);

	//删除上一个+号
	$(obj).hide();
}

//驳回信息框
function boHuiBox(obj)
{
	var val = $(obj).val();

	var html = '\n' +
        '\t\t\t\t<label class="col-sm-2 control-label bo-hui-box">驳回原因</label>\n' +
        '\t\t\t\t<div class="input-group bo-hui-box">\n' +
        '\t\t\t\t\t<div class="col-sm-4">\n' +
        '\t\t\t\t\t\t<textarea name="reason" id="" cols="30" rows="10"></textarea>\n' +
        '\t\t\t\t\t</div>\n' +
        '\t\t\t\t</div>';

	if ( val == -1 )
	{
		//弹出驳回信息框
		$('.bo-hui').append(html);
	}
	else
	{
		//删除信息框
		$('.bo-hui-box').remove();
	}
}

//增加一个特色框
function addTeSe(obj) {
    var html = '\n' +
        '\t\t\t<div class="col-sm-1">\n' +
        '\t\t\t\t<input type="text" name="character[]" value="" class="form-control" placeholder="请输入特色" autocomplete="off" />\n' +
        '\t\t\t</div>\n' +
        '\t\t\t<div class="col-sm-0" onclick="addTeSe(this)">\n' +
        '\t\t\t\t<button type="button" class="btn btn-primary btn-sm btn-flat" >+</button>\n' +
        '\t\t\t</div>';

    if ( $("#addTeSe > .col-sm-1").length >= 4 )
	{
		layer.msg('最多只能有4个特色', {shift : 6});

		return false;
	}

    $('#addTeSe').append(html);

    //删除上一个+号
    $(obj).hide();
}

